#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

void solve()
{
	int n; cin >> n;
	int rk, ck, rd, cd; cin >> rk >> ck >> rd >> cd;
	int dr = abs(rk - rd), dc = abs(ck - cd);
	int base = max(dr, dc);
	int to_r = 0, to_c = 0;
	if (rk > rd) to_r = n - rk;
	if (rk < rd) to_r = rk;
	if (ck > cd) to_c = n - ck;
	if (ck < cd) to_c = ck;
	//if (dr == dc) cout << base + max(to_c, to_r) << endl;
	//if (dr > dc) cout << base + to_r << endl;
	//if (dc > dr) cout << base + to_c << endl;
	
	if (rk > rd) to_r = n - rd;
	if (rk < rd) to_r = rd;
	if (ck > cd) to_c = n - cd;
	if (ck < cd) to_c = cd;
	cout << max(to_r, to_c) << endl;
}

signed main()
{
	int t; cin >> t;
	while (t--) solve();
	return 0;
}

//7 2 0 0 1 1 3 1 1 0 1 1 1 0 0 1 6 1 3 3 2 9 4 1 4 2 82 64 2 63 2 1000000000 500000000 500000000 1000000000 0
